package com.shop2cn.iapi.shenqisettlement.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.GetBankRequiredFieldListReq;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.BankRequiredFieldCountryVo;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.BankRequiredFieldVo;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetBankRequiredFieldListResp;
import com.shop2cn.iapi.shenqisettlement.service.GetBankRequiredFieldListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.shenqisettlement.BankRequiredField;
import com.ymttest.database.sqlwapper.shenqisettlement.BankRequiredFieldWrapper;

public class Ts_GetBankRequiredFieldList {

	private static GetBankRequiredFieldListReq getBankRequiredFieldListReq = null;
	private static GetBankRequiredFieldListCall getBankRequiredFieldListCall = null;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取银行卡必填信息");
	}

	@Before
	public void caseUp() {

		getBankRequiredFieldListReq = new GetBankRequiredFieldListReq();
		getBankRequiredFieldListCall = new GetBankRequiredFieldListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("api/userBandingBankCard");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify(GetBankRequiredFieldListResp resp) {

		GetBankRequiredFieldListResp expectGetBankRequiredFieldListResp = new GetBankRequiredFieldListResp();
		this.constructResponese(expectGetBankRequiredFieldListResp);

		resp.getBankRequiredFieldCountryVoList();

		for (int i = 0; i < resp.getBankRequiredFieldCountryVoList().size(); i++) {

			BankRequiredFieldCountryVo actualBankRequiredFieldCountryVo = resp
					.getBankRequiredFieldCountryVoList().get(i);
			BankRequiredFieldCountryVo expecetBankRequiredFieldCountryVo = expectGetBankRequiredFieldListResp
					.getBankRequiredFieldCountryVoList().get(i);

			Logger.verifyEquals(
					expecetBankRequiredFieldCountryVo.getCountryName(),
					actualBankRequiredFieldCountryVo.getCountryName(),
					"验证countryName");
			Logger.verifyEquals(
					expecetBankRequiredFieldCountryVo.getCountryCode(),
					actualBankRequiredFieldCountryVo.getCountryCode(),
					"验证countryCode");

			for (int j = 0; j < actualBankRequiredFieldCountryVo
					.getBankRequiredFieldVoList().size(); j++) {

				BankRequiredFieldVo actualBankRequiredFieldVo = actualBankRequiredFieldCountryVo
						.getBankRequiredFieldVoList().get(j);
				BankRequiredFieldVo expectBankRequiredFieldVo = expecetBankRequiredFieldCountryVo
						.getBankRequiredFieldVoList().get(j);
				
				Logger.verifyEquals(
						actualBankRequiredFieldVo.getRequiredFieldName(),
						expectBankRequiredFieldVo.getRequiredFieldName(),
						"验证requiredFieldName");
				Logger.verifyEquals(
						actualBankRequiredFieldVo.getRequiredFieldLabel(),
						expectBankRequiredFieldVo.getRequiredFieldLabel(),
						"验证requiredFieldLabel");
				Logger.verifyEquals(
						actualBankRequiredFieldVo.getRequiredFieldDescription(),
						expectBankRequiredFieldVo.getRequiredFieldDescription(),
						"验证requiredFieldDescription");
			}
		}
	}

	private void constructResponese(
			GetBankRequiredFieldListResp expectGetBankRequiredFieldListResp) {

		BankRequiredFieldWrapper wrapper = new BankRequiredFieldWrapper();
		List<BankRequiredField> expectBankRequiredFieldList = wrapper
				.selectByNothing();
		String tempCountryCode = "";
		String tempCountryName = "";
		int i = 0;

		List<BankRequiredFieldVo> bankRequiredFieldVoList = new ArrayList<BankRequiredFieldVo>();
		List<BankRequiredFieldCountryVo> bankRequiredFieldCountryVoList = new ArrayList<BankRequiredFieldCountryVo>();

		for (BankRequiredField expectBankRequiredField : expectBankRequiredFieldList) {

			String countryName = expectBankRequiredField.getCountry_name();
			String countryCode = expectBankRequiredField.getCountry_code();

			// 创建一个新的bankRequiredField
			BankRequiredFieldVo bankRequiredFieldVo = new BankRequiredFieldVo();

			bankRequiredFieldVo.setRequiredFieldName(expectBankRequiredField
					.getRequired_field_name());
			bankRequiredFieldVo.setRequiredFieldLabel(expectBankRequiredField
					.getRequired_field_label());
			bankRequiredFieldVo
					.setRequiredFieldDescription(expectBankRequiredField
							.getRequired_field_description());

			if (!countryName.equalsIgnoreCase(tempCountryName)
					|| !countryCode.equalsIgnoreCase(tempCountryCode)) {

				// 如果是不同的国家，就新建一个bankRequiredFieldVoList并添加一个bankRequiredFieldVo
				bankRequiredFieldVoList = new ArrayList<BankRequiredFieldVo>();
				bankRequiredFieldVoList.add(bankRequiredFieldVo);

				// 如果是不同的国家，就新建一个bankRequiredFieldCountryVo并添加一个bankRequiredFieldVoList
				BankRequiredFieldCountryVo bankRequiredFieldCountryVo = new BankRequiredFieldCountryVo();

				bankRequiredFieldCountryVo.setCountryName(countryName);
				bankRequiredFieldCountryVo.setCountryCode(countryCode);
				bankRequiredFieldCountryVo
						.setBankRequiredFieldVoList(bankRequiredFieldVoList);

				bankRequiredFieldCountryVoList.add(bankRequiredFieldCountryVo);

				i++;
			} else {

				// 如果是相同的国家，就在相同的countryVo中添加bankRequiredFieldVo
				BankRequiredFieldCountryVo bankRequiredFieldCountryVo = bankRequiredFieldCountryVoList
						.get(i - 1);

				bankRequiredFieldCountryVo.getBankRequiredFieldVoList().add(
						bankRequiredFieldVo);
			}

			tempCountryName = countryName;
			tempCountryCode = countryCode;
		}

		// 给期待的结果添加bankRequiredFieldCountryVoList
		expectGetBankRequiredFieldListResp
				.setBankRequiredFieldCountryVoList(bankRequiredFieldCountryVoList);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBankRequiredFieldList_001() {
		Logger.start(true, "获取银行卡必填信息");
		try {

			getBankRequiredFieldListReq.setUserId(502060981);

			getBankRequiredFieldListCall.setData(getBankRequiredFieldListReq);
			getBankRequiredFieldListCall.callService();

			verify(getBankRequiredFieldListCall.getResult());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
